//
//  CActionSpriteW.mm
//  Cocos2dAction
//
//  Created by Hunk on 10-6-28.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CActionSpriteW.h"
#import "Role.h"

#import "CAnimationPlayW.h"
@implementation CActionSpriteW

@synthesize __pCCAnimation,__bCys;

+(id)node
{
	return [super node];
//	__currentFrame = 0;
//	__actionIndex = 0;
}

-(void)InitChildSprite:(int) ActionIndex Animation:(CAnimationPlayW*)pAnimation  cyc:(BOOL)IsCys
{
	//add 10 sprite in this sprite 
	for(int i=0;i <10;i++)
	{
		CCSprite * pS = [CCSprite node];
		[pS retain];
		
		[self addChild:pS  z: i tag:i];
	}
	
	__actionIndex = ActionIndex;
	
	__pCCAnimation = pAnimation;
	
	__bCys = IsCys;
}


-(void)InitFrameTime:(float)fTime
{
	__frameTime = fTime;
}

-(void)StartPlay
{
	__frameTime = 0.3;
	__currentFrame = 0;
	__isInPlay = true;
	visible_ = true;
	[self schedule:@selector(PlayNextFrame:) interval:(__frameTime/((float)__doubleTime))];
}

-(void)PlayNextFrame :(ccTime)dt
{
	//set the sprite able display;
	//visible_ = true;
	__isInPlay = true;
	//get the role read from file.
	Role * pRole = (Role*)[__pCCAnimation __currentRole];
	vector<Action*>&ActionVec  = pRole->GetActionVec();
	vector<Frame*> & FrameVec = ActionVec[__actionIndex]->GetActionFrame();
	Frame * pCurrentFrame = FrameVec[__currentFrame];
	
	byte clipNum = pCurrentFrame->m_Num;
	
	for(int i=0; i< clipNum;i++)
	{
		byte imgId = *(pCurrentFrame->imgID+i);
		NSString * strName = [[__pCCAnimation __imageNameArray] objectAtIndex:imgId];
		int xOff = *(pCurrentFrame->m_iX + i);
		int width = *(pCurrentFrame->m_iClipW + i);
		int height =  *(pCurrentFrame->m_iClipH + i);
		int yOff = -* (pCurrentFrame->m_iY + i)-height;
		int xPic =  *(pCurrentFrame->m_iClipX + i);
		int yPic =  *(pCurrentFrame->m_iClipY + i);
		CGRect rect = CGRectMake(xPic, yPic, width, height);
		CGPoint clipPosition = CGPointMake(__MainPoint.x + width/2+xOff,__MainPoint.y+ height/2+yOff);
		
		//NSLog(@"current rect:%d,%d,%d,%d,",xPic,yPic,width,height);

		//get the texture this frame used
		CCTexture2D* pTex =[ [CCTextureCache sharedTextureCache]addImage:strName];
		//change the sprite used texture
		[(CCSprite*)[self getChildByTag:i] setTexture:pTex];
		[(CCSprite*)[self getChildByTag:i] setTextureRect:rect];
		[self getChildByTag:i].position =  clipPosition;
		[[self getChildByTag:i] setVisible:true];
	}
	__frameTime =pCurrentFrame->m_iTime;
	for(int i=clipNum;i<10;i++)
	{
		
		[(CCSprite*)[self getChildByTag:i] setVisible:false];
		 
	}
	
	__currentFrame++;
	
	if((unsigned int)__currentFrame >=(FrameVec.size()-1) )
	{
		__isInPlay = false;
		__currentFrame = 0;
		
		if(__bCys == false)
		{
			visible_= false;
			[self setVisible:NO];
			return;
		}
	}
	
	float backTime = __frameTime/1000;
	
	[self schedule:@selector(PlayNextFrame:) interval:(backTime/__doubleTime)];
							  
}

-(void)SpeedUpPlay:(int)doubleTime
{
	__doubleTime = doubleTime;
	
}

-(void)setVisible:(BOOL)v
{
	[super setVisible:v];
	if(v == NO)
	{
		[self unschedule:@selector(PlayNextFrame:)];
	}
	else
	{
		[self StartPlay];
	}

}

-(BOOL)IsPlayOver
{
	return !__isInPlay;
}
@end
